<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" v-auth="'goods.spu:uf_mall_goods:add'" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增 </a-button>
        <a-button type="primary" v-auth="'goods.spu:uf_mall_goods:exportXls'" preIcon="ant-design:export-outlined" @click="onExportXls">
          导出
        </a-button>
        <j-upload-button type="primary" v-auth="'goods.spu:uf_mall_goods:importExcel'" preIcon="ant-design:import-outlined" @click="onImportXls">
          导入
        </j-upload-button>
        <j-upload-button type="primary" v-auth="'goods.spu:uf_mall_goods:importExcel'" preIcon="ant-design:import-outlined" @click="handleZipUpload">
          导入压缩图片包
        </j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item v-auth="'goods.spu:uf_mall_goods:edit'" key="1" @click="batchHandleUpdate(1)">
                <Icon icon="ant-design:cloud-upload-outlined" />
                批量上架
              </a-menu-item>
              <a-menu-item v-auth="'goods.spu:uf_mall_goods:edit'" key="2" @click="batchHandleUpdate(0)">
                <Icon icon="ant-design:cloud-download-outlined" />
                批量下架
              </a-menu-item>
              <a-menu-item v-auth="'goods.spu:uf_mall_goods:deleteBatch'" key="3" @click="batchHandleDelete()">
                批量删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button v-auth="'goods.spu:uf_mall_goods:edit'">
            批量操作
            <Icon icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
        <!-- 高级查询 -->
        <super-query :config="superQueryConfig" @search="handleSuperQuery" />
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #bodyCell="{ column, record, index, text }"></template>
    </BasicTable>
    <!-- 表单区域 -->
    <UfMallGoodsModal @register="registerDrawer" @success="handleSuccess" />
    <UfMallSpuSkuDrawer @register="registerSpuSkuDrawer" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="goods.spu-ufMallGoods" setup>
  import { ref, reactive, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useDrawer } from '/@/components/Drawer';
  import { useListPage } from '/@/hooks/system/useListPage';
  import UfMallGoodsModal from './components/UfMallGoodsModal.vue';
  import { columns, searchFormSchema, superQuerySchema } from './UfMallGoods.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, saveOrUpdate, getZipImportUrl, batchUpdate } from './UfMallGoods.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useUserStore } from '/@/store/modules/user';
  import UfMallSpuSkuDrawer from '@/views/goods/sku/components/UfMallSpuSkuDrawer.vue';
  import { defHttp } from '@/utils/http/axios';

  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  //注册model
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerSpuSkuDrawer, { openDrawer: openSpuSkuDrawer }] = useDrawer();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '商品信息管理',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '商品信息管理',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);

  async function handleZipUpload(data) {
    await defHttp.uploadFile({ url: getZipImportUrl }, { file: data.file });
  }

  function batchHandleUpdate(status) {
    batchUpdate({ ids: selectedRowKeys.value, status }, reload);
  }

  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    reload();
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openDrawer(true, {
      isUpdate: false,
      showFooter: true,
    });
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 上下架
   * @param record
   * @param status
   */
  function handleModifyStatus(record, status) {
    let form: any = {};
    form.id = record.id;
    form.status = status;
    saveOrUpdate(form, true).then(() => {
      handleSuccess();
    });
  }

  function handleOpenSkuDrawer(record: any) {
    openSpuSkuDrawer(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        auth: 'goods.spu:uf_mall_goods:edit',
        ifShow: !record.status,
      },
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '上架',
        onClick: handleModifyStatus.bind(null, record, 1),
        ifShow: !record.status,
      },
      {
        label: '下架',
        onClick: handleModifyStatus.bind(null, record, 0),
        ifShow: !!record.status,
      },
      {
        label: 'SKU管理',
        onClick: handleOpenSkuDrawer.bind(null, record, 0),
        ifShow: !record.status,
      },
    ];
  }
</script>

<style lang="less" scoped>
  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
